/**
 * 关于对于request的异步请求的封装
 * auther：xianghai
 * time:2020 12 31
 * 
 */
export class Request {
	constructor(arg) {
		this.Domain = "http://cp.maiweihunli.com/api"; //接口地址
		this.networkStatus = true; //网络状态的存储
		this.interface_url = {
			loginApi: "/common/login", //登录接口username用户名,password密码(md5加密后
			register: "/common/register", //用户注册username用户名,phone手机号,password密码,code邀请码
			appStart: "/index/api/appStart", //app启动检查
			recharge: "/user/SubRecharge", //提交充值   money:充值金额
			applay: "/Withdrawal/submit", // 申请提现     money提现金额, type0支付宝 1银行卡
			recharging: "/Withdrawal/ChangeBonus", //  转充   money转充金额
			selectCode: "/user/InvitationCode", //  查看用户邀请码
			football: "/common/LotteryListFootball", //足球下单列表
			userInfo: "/user/info", //用户的信息
		};
		this.userData = { //用户数据
			language: 1,
			user_id: "-1", //用户的id
			isLogin: 'false', //登录状态
			update_data: "1.0", //更新的版本
			startTimes: -1, //启动次数
			p: "", //解密秘钥
		};
		this.timeout=5000;//请求超时
		//获取网络状态
		uni.getNetworkType({
			success: function(res) {
				(res.networkType == 'none') ? (this.networkStatus = false) : "";

			}
		});



	}
	/**
	 * 更新用户数据
	 */
	updateUserData() {
		console.log("用户数据：" + JSON.stringify(this.userData));
		return new Promise((resolve, reject) => {
			uni.setStorage({
				key: 'userData',
				data: this.userData,
				success: () => {
					console.log("存储数据成功");
					resolve();
				},
				fail: () => {
					console.log("存储失败");
					resolve();
				}
			})
		})
	}


	/**
	 * 消失提示
	 * @param {Object} content
	 */
	showToast(content) {
		uni.hideLoading();
		uni.showToast({
			title: content,
			icon: "none"
		});

	}
	/**
	 * 网络断开的提示
	 */
	tishi() {
		uni.showModal({
			title: "提示",
			content: "网络似乎断开了",
			showCancel: false,
			success: (e) => {
				console.log(e);
			}
		})
	}
	/**
	 * 日期转换时间时间戳转换时间
	 * @param {Object} time
	 */
	to_date(time, type) {
		let str = parseInt(time) * 1000;
		let newDate = new Date(str);
		let year = newDate.getUTCFullYear();
		let month = newDate.getUTCMonth();
		let nowday = newDate.getUTCDate();
		let hours = newDate.getHours(); //取小时
		let minutes = newDate.getMinutes(); //取分钟
		let seconds = newDate.getSeconds(); //取秒
		hours = (hours < 10) ? "0" + hours : hours;
		minutes = (minutes < 10) ? "0" + minutes : minutes;
		let typeArr = {
			0: year + " " + month + "." + nowday + "" + " " + hours + ":" + minutes,
			1: year + "." + month + "." + nowday,
		}
		return typeArr[type];
	}
	/**
	 * 判断是否登录，未登录直接跳转到登录授权界面
	 */
	isLogin(callback) {

		uni.getStorage({
			key: 'userData',
			success: (res) => {
				callback(true);
			},
			fail: () => {
				uni.navigateTo({
					url: "/pages/user/login",
					animationType: 'slide-in-bottom'
				});
				callback(false);
			}
		})
	}
	/**
	 * 检查登录
	 * @param {Object} callback
	 */
	checkLogin(callback) {
		uni.getStorage({
			key: "session",
			success: (res) => {
				if (res.data.code == 101) {
					callback(false);
				} else {
					this.isLogin = true;
					this.sessionData = res;
					callback(true);
				}

			},
			fail: (err) => {
				callback(false);
			}
		});
	}


	/**
	 * 关于异步请求的封装 无需登录信息附带
	 * @param {Object} url 请求的地址
	 * @param {Object} type 请求的类型
	 * @param {Object} data  请求的参数
	 */
	request(url, type, data,ishow) {
		this.isLogin((r)=>{
			
		});
		let token = uni.getStorageSync('token');
		let header = {
			'Content-Type': 'application/json',
			'token':token
		};
		return new Promise((resolve, reject) => {
			uni.request({
				url: this.Domain + url,
				method: type,
				data: data,
				timeout:this.timeout,
				header: header,
				success: res => {
					switch (res.data.status) {
						case 221:
							uni.removeStorage({
								key: "userData"
							});
							uni.navigateTo({
								url: "/pages/user/login",
								animationType: 'slide-in-bottom'
							});
							break;
					}
					resolve(res.data);
				},
				fail: (e) => {
					reject(e);
					if(ishow){
						this.showToast("请求超时");
					}
				    
				},
				complete: () => {}
			});

		});

	}
	/**
	 * 普通接口的数据请求
	 * 无须处于登录状态
	 * @param {Object} url
	 * @param {Object} type
	 * @param {Object} data
	 */
	common(url, type, data) {
		return new Promise((resolve, reject) => {
			uni.request({
				url: this.Domain + url,
				method: type,
				data: data,
				header: {
					'Content-Type': 'application/json'
				},
				success: res => {
					resolve(res.data);
				},
				fail: (e) => {
					reject(e);
				},
				complete: () => {}
			});

		});
	}

	/**
	 *  "服务器开小差了";
	 */
	server_err() {

		return "服务器开小差了";

	}


}
